package ch.fusun.baron.serverapp.view;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import ch.fusun.baron.core.injection.ReInjector;
import ch.fusun.baron.serverapp.configuration.ConfigurationPart;

/**
 * View to configure stuff man! HOLY COW
 */
public class ConfigurationView extends ViewPart {

	private static final String CONFIGURATION_ID = "ch.fusun.baron.serverapp.configuration"; //$NON-NLS-1$
	private static final String CONFIGURATION = "configuration"; //$NON-NLS-1$
	private final List<ConfigurationPart> configurationParts = new LinkedList<ConfigurationPart>();

	/**
	 * Constructor
	 */
	public ConfigurationView() {
		IConfigurationElement[] config = Platform.getExtensionRegistry()
				.getConfigurationElementsFor(CONFIGURATION_ID);
		try {
			for (IConfigurationElement e : config) {
				ConfigurationPart bean = (ConfigurationPart) e
						.createExecutableExtension(CONFIGURATION);
				configurationParts.add(bean);
				ReInjector.getInstance().reInject(bean);
			}
		} catch (CoreException ex) {
			System.err.println("Handle this error with log or so: " //$NON-NLS-1$
					+ ex.getMessage());
		}
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite area = new Composite(parent, SWT.NONE);
		area.setLayout(new GridLayout());
		for (ConfigurationPart part : configurationParts) {
			part.createPartControl(area);
		}
	}

	@Override
	public void setFocus() {
		// Do nothing
	}

}
